﻿using System.Collections.Generic;
using System.Linq;

namespace EntityFramework
{
    public class ZoneProductBO : BaseBO
    {
        public static List<ZoneProduct> GetListZoneProduct()
        {
            using (var _db = new DbWebsite())
            {
                return _db.StoredProcedures.GetListZoneProduct();
            }
        }

        public static List<ZoneProduct> GetListZoneProductByCompanyId(int companyId)
        {
            using (var _db = new DbWebsite())
            {
                return _db.StoredProcedures.GetListZoneProductByCompanyId(companyId);
            }
        }

        public static ZoneProduct GetZoneProductDetailByUrl(string url)
        {
            using (var _db = new DbWebsite())
            {
                return _db.StoredProcedures.GetZoneProductDetailByUrl(url);
            }
        }

        public static List<ZoneProduct> GetListZoneProduct(int parentId)
        {
            return db.ZoneProducts.Where(d => d.status == 1 && d.ParentId == parentId).OrderBy(d => d.title).ToList();
        }

        public static string GetZoneProductNameById(int id)
        {
            var singleOrDefault = GetListZoneProduct().SingleOrDefault(d => d.ZoneId == id);
            return singleOrDefault != null ? singleOrDefault.title : string.Empty;
        }

        public static string GetZoneProductNameByShortUrl(string shortUrl)
        {
            var singleOrDefault = GetListZoneProduct().SingleOrDefault(d => d.url == shortUrl);
            return singleOrDefault != null ? singleOrDefault.title : string.Empty;
        }

        public static List<ZoneProduct> GetListZoneProductByParentId(int id)
        {
            return GetListZoneProduct().Where(d => d.ParentId == id).OrderBy(d => d.ZoneId).ToList();
        }

        public static ZoneProduct GetZoneProductByCatUrl(string catUrl)
        {
            var singleOrDefault = GetListZoneProduct().SingleOrDefault(d => d.url == catUrl);
            return singleOrDefault ?? null;
        }

        public static ZoneProduct GetZoneProductById(int id)
        {
            var singleOrDefault = GetListZoneProduct().SingleOrDefault(d => d.ZoneId == id);
            return singleOrDefault ?? null;
        }

        public static string GetZoneUrlByShortUrl(string shortUrl)
        {
            var href = string.Empty;
            var singleOrDefault = GetListZoneProduct().SingleOrDefault(d => d.url == shortUrl);
            if (singleOrDefault.ParentId > 0)
            {
                var parent = GetListZoneProduct().SingleOrDefault(d => d.ZoneId == singleOrDefault.ParentId);
                href = "/" + parent.url + "/" + singleOrDefault.url + ".htm";
            }
            else
            {
                href = "/" + singleOrDefault.url + ".htm";
            }
            return href != null ? href : string.Empty;
        }


    }
}
